package com.caregiver.watch.business.data.parser;

import com.google.protobuf.InvalidProtocolBufferException;
import com.caregiver.watch.business.data.protobuf.SedentarinessOuterClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 久坐数据解析
 */
@Component
@Slf4j
public class SedentaryDataParser implements IDataParser {

    @Override
    public Integer getOpt() {
        return null;
    }

    @Override
    public void dataParse(Long userId, String deviceId, byte[] payload) {
        SedentarinessOuterClass.SedtNotification sedtNotify = null;
        try {
            sedtNotify = SedentarinessOuterClass.SedtNotification.parseFrom(payload);
        } catch (InvalidProtocolBufferException ex) {
            ex.printStackTrace();
            return;
        }

        SedentarinessOuterClass.SedtGroup sedtGroup = sedtNotify.getGroup();
        if (sedtGroup != null) {
            SedentarinessOuterClass.SedtOperation operation = sedtNotify.getOperation();
            if (sedtGroup.getSedentarinessCount() == 0) {
                return;
            }
            List<SedentarinessOuterClass.Sedentariness> sedtList = sedtGroup.getSedentarinessList();
            for (int i = 0; i < sedtList.size(); i++) {
                SedentarinessOuterClass.Sedentariness sedt = sedtList.get(i);
                boolean repeat = sedt.getRepeat();
                StringBuffer sb = new StringBuffer();
                boolean monday = sedt.getMonday();
                if (monday) {
                    sb.append("monday|");
                }
                boolean tuesday = sedt.getTuesday();
                if (tuesday) {
                    sb.append("tuesday|");
                }
                boolean wednesday = sedt.getWednesday();
                if (wednesday) {
                    sb.append("wednesday|");
                }
                boolean thursday = sedt.getThursday();
                if (thursday) {
                    sb.append("thursday|");
                }
                boolean friday = sedt.getFriday();
                if (friday) {
                    sb.append("friday|");
                }
                boolean saturday = sedt.getSaturday();
                if (saturday) {
                    sb.append("saturday|");
                }
                boolean sunday = sedt.getSunday();
                if (sunday) {
                    sb.append("sunday|");
                }
                String weekday = "none";
                if (sb.length() > 0) {
                    weekday = sb.toString();
                    weekday = weekday.substring(0, weekday.length() - 1);
                }
                int start_hour = sedt.getStartHour();
                int end_hour = sedt.getEndHour();
                int duration = sedt.getDuration();
                int threshold = sedt.getThreshold();
                log.info("sedentary setting, {}, {}, from {} - {}, duration {}, threshold:{}",
                        repeat ? "repeated" : "not repeated", weekday, start_hour, end_hour, duration, threshold);
            }
        }

    }
}
